package com.JDK新特性;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

public class Stream流
{
    List<Employee> list = Arrays.asList(new Employee(101, "林青霞", 28, 9889.99), new Employee(102, "东方不败", 29, 4329.85), new Employee(103, "周星驰", 40, 1233.88), new Employee(104, "大圣", 500, 5000.44), new Employee(105, "张无忌", 15, 3000.09), new Employee(102, "东方不败", 29, 4329.85));
    List<Employee> emps = list;
//    我在Master上也加了一个注解
    public static void main(String[] args)
    {
        Stream<Integer> stream1 = Stream.iterate(0, (x) -> x + 2).limit(4);
        stream1.forEach(System.out::println);

        System.out.println("-------");

        Stream<Double> stream2 = Stream.generate(Math::random).limit(4);
        stream2.forEach(System.out::println);

        System.out.println("--------");
    }

    @Test
    public void testFilter()
    {
        Stream<Employee> stream = list.stream().filter((e) -> {
            System.out.println("惰性求值");
            return e.getAge() < 30;
        });
        System.out.println("-----");
//        终止操作，一次性执行全部功能，称为惰性求值
        stream.forEach(System.out::println);
    }

//    截断流
    @Test
    public void test1()
    {
        emps.stream().filter(e -> e.getAge() < 30).limit(2).forEach(System.out::println);
    }

//    跳过
    @Test
    public void test5() {
        emps.stream()
                .filter(employee -> employee.getAge() < 30)
                .skip(2)
                .forEach(System.out::println);
    }

//    去重
    @Test
    public void test6() {
        emps.stream()
                .distinct()
                .forEach(System.out::println);
    }

//    map操作
    @Test
    public void test7() {
        List<String> list = Arrays.asList("aaa", "java", "ccc", "java8", "hello world");

        list.stream()
                .map((x) -> x.toUpperCase())
                .forEach(System.out::println);

        System.out.println("-------------");

        emps.stream()
                .map(Employee::getAge)
                .forEach(System.out::println);
    }

    @Test
    public void test9() {
        emps.stream()
                .map(Employee::getNumber)
                .sorted()
                .forEach(System.out::println);

        System.out.println("-----------------");

        emps.stream()
                .map(Employee::getAge)
                .sorted(Integer::compare)
                .forEach(System.out::println);
    }



}


class Employee
{
    public int id;
    public String name;
    public int age;
    public double number;

    @Override
    public String toString()
    {
        return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", number=" + number + '}';
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o == null || getClass() != o.getClass())
        {
            return false;
        }
        Employee employee = (Employee) o;
        return id == employee.id && age == employee.age && Double.compare(employee.number, number) == 0 && name.equals(employee.name);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(id, name, age, number);
    }

    public Employee()
    {
    }

    public Employee(int id, String name, int age, double number)
    {
        this.id = id;
        this.name = name;
        this.age = age;
        this.number = number;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public double getNumber()
    {
        return number;
    }

    public void setNumber(double number)
    {
        this.number = number;
    }
}
